package com.ourstocks.test.module.tecentspider;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.jsoup.Jsoup;
import org.junit.Test;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoClient;
import com.ourstocks.common.util.stock.StockDateUtil;
import com.ourstocks.common.util.stock.StockSpider;
import com.ourstocks.module.maindata.po.Stock;
import com.ourstocks.module.maindata.po.StockCodeInfo;

public class SHStockPriceTestOf {
	
    private static final int timeout = 80000;
    private static final String MONGODBHOST = "127.0.0.1";
    private static final int MONGODBPORT = 27017;
    private static final String DATABASENAME = "westock";
    private static final MongoTemplate mongoTemplate = new MongoTemplate(new MongoClient(MONGODBHOST, MONGODBPORT), DATABASENAME);
    
    @Test
    public void execute() throws JobExecutionException {
        /*if (!StockDateUtil.stockOpenTime()) {//不开市时直接返回
            return;
        }*/
        Long start = System.currentTimeMillis();
        List<StockCodeInfo> codes = mongoTemplate.find(new Query(Criteria.where("type").is("sh")), StockCodeInfo.class);//沪市   1461条
        List<String> list = new ArrayList<>();
        for (int i = 0; i < codes.size(); i++) {
            list.add(String.format("%s%s",codes.get(i).getType(),codes.get(i).getCode()));
            if (i > 0 && (i % 300 == 0 || i == codes.size() - 1)) {
                try {
                    this.updateStockPrice(list);
                    list = new ArrayList<>();
                    TimeUnit.MILLISECONDS.sleep(800);
                } catch (Exception ex) {
                    System.out.printf("{%s}", ex);
                    ex.printStackTrace();
                }
            }

        }
        System.out.printf("沪市数据更新共：{%s}只股票,更新耗时：{%s}ms",codes.size(),(System.currentTimeMillis() - start));
    }
    
    /**
     * 更新股票价格
     * @param codes
     * @param spider
     * @throws Exception
     */
    public void updateStockPrice(List<String> codes) throws Exception {
        JSONArray jsons = this.getStockPrice(codes.toArray(new String[]{}));
        if (jsons !=null) {//防止出现空指针异常
        	List<Stock> stocks = JSON.parseArray(jsons.toJSONString(), Stock.class);
        	stocks.stream().forEach(stock -> {
        		mongoTemplate.upsert(
        				new Query(Criteria.where("_id").is(stock.getCode())),
        				new Update()
        				.set("_id", stock.getCode())
        				.set("type", stock.getType())
        				.set("name", stock.getName())
        				.set("price", stock.getPrice())
        				.set("yesterdayPrice", stock.getYesterdayPrice())
        				.set("fluctuate", stock.getFluctuate())
        				.set("todayMax", stock.getTodayMax())
        				.set("todayMin", stock.getTodayMin())
        				.set("priceDate", stock.getPriceDate()),
        				"stock"
        				);
        	});
        }
    }
    
	 public JSONArray getStockPrice(String[] stockCode) throws Exception {
	        String codes= Arrays.stream(stockCode).collect(Collectors.joining(","));
	        String api =String.format("http://qt.gtimg.cn/q=%s",codes);
	        String content = null;
	        try {
	            content = Jsoup.connect(api).timeout(timeout).ignoreContentType(true).get().text();
	            List<String> rows =Arrays.asList(content.split(";"));
	            JSONArray jsonArray = new JSONArray();
	            JSONObject json;
	            for (int i = 0; i < rows.size(); i++) {
	                String [] item =rows.get(i).replaceAll("v_.*=|\"","").split("~");
	                json= new JSONObject();
	                //代码
	                json.put("code", item[2]);
	                //波动
	                json.put("fluctuate", item[32]);
	                json.put("type", StockSpider.formatStockCode(item[2]).replaceAll("\\d", ""));
	                //名称
	                json.put("name", item[1].replaceAll("\\s", ""));
	                //现价
	                json.put("price", item[3]);
	                //今日最高价
	                json.put("todayMax", item[41]);
	                //今日最低价
	                json.put("todayMin", item[42]);
	                //昨收
	                json.put("yesterdayPrice", item[4]);
	                json.put("priceDate", StockDateUtil.getCurrentDateTimeNumber());
	                jsonArray.add(json);
	            }
	            return jsonArray;
	        } catch (SocketTimeoutException e) {
	            e.printStackTrace();
	            return null;
	        }
	    }
}
